﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Game.Global
{
    public class GameObjVisibleHandler : MonoBehaviour
    {
        public event CallbackFunc cbOnObjIsVisible;
        public event CallbackFunc cbOnObjIsInvisible;

        /// <summary>
        /// 给物体添加一个Handler，掌控Visible和Invisible事件
        /// </summary>
        /// <param name="objWithMesh">Object with mesh.</param>
        /// <param name="cbOnVisible">Cb on became visible.</param>
        /// <param name="cbOnInvisible">Cb on became invisible.</param>
        public static void SetHandler(Renderer objWithMesh, CallbackFunc cbOnVisible, CallbackFunc cbOnInvisible)
        {
            var handler = objWithMesh.gameObject.AddComponent<GameObjVisibleHandler>();
            handler.cbOnObjIsVisible = cbOnVisible;
            handler.cbOnObjIsInvisible = cbOnInvisible;
        }

        private void OnBecameVisible()
        {
            cbOnObjIsVisible();
        }

        private void OnBecameInvisible()
        {
            cbOnObjIsInvisible();
        }
    }
}